<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Dimensional Analysis</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../boost_units.html" title="Chapter 44. Boost.Units 1.1.0">
<link rel="prev" href="Quick_Start.html" title="Quick Start">
<link rel="next" href="Units.html" title="Units">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="Quick_Start.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_units.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="Units.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_units.Dimensional_Analysis"></a><a class="link" href="Dimensional_Analysis.html" title="Dimensional Analysis">Dimensional Analysis</a>
</h2></div></div></div>
<p>
      The concept of <a href="http://en.wikipedia.org/wiki/Dimensional_analysis" target="_top">dimensional
      analysis</a> is normally presented early on in introductory physics and
      engineering classes as a means of determining the correctness of an equation
      or computation by propagating the physical measurement <a href="http://en.wikipedia.org/wiki/Units_of_measurement" target="_top">units</a>
      of various quantities through the equation along with their numerical values.
      There are a number of standard unit systems in common use, the most prominent
      of which is the <a href="http://en.wikipedia.org/wiki/SI_units" target="_top">Systeme
      International</a> (also known as SI or MKS (meter-kilogram-second), which
      was a metric predecessor to the SI system named for three of the base units
      on which the system is based). The SI is the only official international standard
      unit system and is widely utilized in science and engineering. Other common
      systems include the <a href="http://en.wikipedia.org/wiki/Cgs_units" target="_top">CGS</a>
      (centimeter-gram-second) system and the <a href="http://en.wikipedia.org/wiki/English_units" target="_top">English</a>
      system still in use in some problem domains in the United States and elsewhere.
      In physics, there also exist a number of other systems that are in common use
      in specialized subdisciplines. These are collectively referred to as <a href="http://en.wikipedia.org/wiki/Natural_units" target="_top">natural units</a>. When
      quantities representing different measurables are combined, dimensional analysis
      provides the means of assessing the consistency of the resulting calculation.
      For example, the sum of two lengths is also a length, while the product of
      two lengths is an area, and the sum of a length and an area is undefined. The
      fact that the arguments to many functions (such as exp, log, etc...) must be
      dimensionless quantities can be easily demonstrated by examining their series
      expansions in the context of dimensional analysis. This library facilitates
      the enforcement of this type of restriction in code involving dimensioned quantities
      where appropriate.
    </p>
<p>
      In the following discussion we view dimensional analysis as an abstraction
      in which an arbitrary set of <a href="http://en.wikipedia.org/wiki/Fundamental_units" target="_top">units</a>
      obey the rules of a specific algebra. We will refer to a pair of a base dimension
      and a rational exponent as a <span class="bold"><strong>fundamental dimension</strong></span>,
      and a list composed of an arbitrary number of fundamental dimensions as a
      <span class="bold"><strong>composite dimension</strong></span> or, simply, <span class="bold"><strong>dimension</strong></span>. In particular, given a set of <span class="inlinemediaobject"><img src="../../../libs/units/images/form_0.png" alt="form_0"></span> fundamental dimensions denoted by <span class="inlinemediaobject"><img src="../../../libs/units/images/form_1.png" alt="form_1"></span> and a set of <span class="inlinemediaobject"><img src="../../../libs/units/images/form_0.png" alt="form_0"></span> rational exponents <span class="inlinemediaobject"><img src="../../../libs/units/images/form_2.png" alt="form_2"></span>, any possible (composite) dimension can be written as
      <span class="inlinemediaobject"><img src="../../../libs/units/images/form_3.png" alt="form_3"></span>.
    </p>
<p>
      Composite dimensions obey the algebraic rules for dimensional analysis. In
      particular, for any scalar value, <span class="inlinemediaobject"><img src="../../../libs/units/images/form_4.png" alt="form_4"></span>, and composite dimensions <span class="inlinemediaobject"><img src="../../../libs/units/images/form_5.png" alt="form_5"></span> and <span class="inlinemediaobject"><img src="../../../libs/units/images/form_6.png" alt="form_6"></span>, where <span class="inlinemediaobject"><img src="../../../libs/units/images/form_7.png" alt="form_7"></span>, we have:
    </p>
<p>
      <span class="inlinemediaobject"><img src="../../../libs/units/images/form_8.png" alt="form_8"></span>
    </p>
<p>
      Users of a dimensional analysis library should be able to specify an arbitrary
      list of base dimensions to produce a composite dimension. This potentially
      includes repeated tags. For example, it should be possible to express energy
      as <span class="inlinemediaobject"><img src="../../../libs/units/images/form_9.png" alt="form_9"></span>, <span class="inlinemediaobject"><img src="../../../libs/units/images/form_10.png" alt="form_10"></span>, <span class="inlinemediaobject"><img src="../../../libs/units/images/form_11.png" alt="form_11"></span>, or any other permutation of mass, length, and time having
      aggregate exponents of 1, 2, and -2, respectively. In order to be able to perform
      computations on arbitrary sets of dimensions, all composite dimensions must
      be reducible to an unambiguous final composite dimension, which we will refer
      to as a <span class="bold"><strong>reduced dimension</strong></span>, for which
    </p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
          fundamental dimensions are consistently ordered
        </li>
<li class="listitem">
          dimensions with zero exponent are elided. Note that reduced dimensions
          never have more than <span class="inlinemediaobject"><img src="../../../libs/units/images/form_0.png" alt="form_0"></span> base dimensions, one for each distinct fundamental
          dimension, but may have fewer.
        </li>
</ol></div>
<p>
      In our implementation, base dimensions are associated with tag types. As we
      will ultimately represent composite dimensions as typelists, we must provide
      some mechanism for sorting base dimension tags in order to make it possible
      to convert an arbitrary composite dimension into a reduced dimension. For this
      purpose, we assign a unique integer to each base dimension. The <span class="underline"><code class="computeroutput"><a class="link" href="../boost/units/base_dimension.html" title="Class template base_dimension">base_dimension</a></code></span> class
      (found in <code class="computeroutput"><a class="link" href="Reference.html#header.boost.units.base_dimension_hpp" title="Header &lt;boost/units/base_dimension.hpp&gt;">boost/units/base_dimension.hpp</a></code>)
      uses the curiously recurring template pattern (CRTP) technique to ensure that
      ordinals specified for base dimensions are unique:
    </p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Derived</span><span class="special">,</span> <span class="keyword">long</span> <span class="identifier">N</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">base_dimension</span> <span class="special">{</span> <span class="special">...</span> <span class="special">};</span>
</pre>
<p>
      With this, we can define the base dimensions for length, mass, and time as:
    </p>
<p>
</p>
<pre class="programlisting"><span class="comment">/// base dimension of length</span>
<span class="keyword">struct</span> <span class="identifier">length_base_dimension</span> <span class="special">:</span> <span class="identifier">base_dimension</span><span class="special">&lt;</span><span class="identifier">length_base_dimension</span><span class="special">,</span><span class="number">1</span><span class="special">&gt;</span> <span class="special">{</span> <span class="special">};</span>
<span class="comment">/// base dimension of mass</span>
<span class="keyword">struct</span> <span class="identifier">mass_base_dimension</span> <span class="special">:</span> <span class="identifier">base_dimension</span><span class="special">&lt;</span><span class="identifier">mass_base_dimension</span><span class="special">,</span><span class="number">2</span><span class="special">&gt;</span> <span class="special">{</span> <span class="special">};</span>
<span class="comment">/// base dimension of time</span>
<span class="keyword">struct</span> <span class="identifier">time_base_dimension</span> <span class="special">:</span> <span class="identifier">base_dimension</span><span class="special">&lt;</span><span class="identifier">time_base_dimension</span><span class="special">,</span><span class="number">3</span><span class="special">&gt;</span> <span class="special">{</span> <span class="special">};</span>
</pre>
<p>
    </p>
<p>
      It is important to note that the choice of order is completely arbitrary as
      long as each tag has a unique enumerable value; non-unique ordinals are flagged
      as errors at compile-time. Negative ordinals are reserved for use by the library.
      To define composite dimensions corresponding to the base dimensions, we simply
      create MPL-conformant typelists of fundamental dimensions by using the <span class="underline"><code class="computeroutput"><a class="link" href="../boost/units/dim.html" title="Struct template dim">dim</a></code></span>
      class to encapsulate pairs of base dimensions and <span class="underline"><code class="computeroutput"><a class="link" href="../boost/units/static_rational.html" title="Class template static_rational">static_rational</a></code></span>
      exponents. The <span class="underline"><code class="computeroutput"><a class="link" href="../boost/units/make_dimension_list.html" title="Struct template make_dimension_list">make_dimension_list</a></code></span>
      class acts as a wrapper to ensure that the resulting type is in the form of
      a reduced dimension:
    </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">make_dimension_list</span><span class="special">&lt;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span> <span class="identifier">dim</span><span class="special">&lt;</span> <span class="identifier">length_base_dimension</span><span class="special">,</span><span class="identifier">static_rational</span><span class="special">&lt;</span><span class="number">1</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="special">&gt;</span>
<span class="special">&gt;::</span><span class="identifier">type</span>   <span class="identifier">length_dimension</span><span class="special">;</span>

<span class="keyword">typedef</span> <span class="identifier">make_dimension_list</span><span class="special">&lt;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span> <span class="identifier">dim</span><span class="special">&lt;</span> <span class="identifier">mass_base_dimension</span><span class="special">,</span><span class="identifier">static_rational</span><span class="special">&lt;</span><span class="number">1</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="special">&gt;</span>
<span class="special">&gt;::</span><span class="identifier">type</span>     <span class="identifier">mass_dimension</span><span class="special">;</span>

<span class="keyword">typedef</span> <span class="identifier">make_dimension_list</span><span class="special">&lt;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span> <span class="identifier">dim</span><span class="special">&lt;</span> <span class="identifier">time_base_dimension</span><span class="special">,</span><span class="identifier">static_rational</span><span class="special">&lt;</span><span class="number">1</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="special">&gt;</span>
<span class="special">&gt;::</span><span class="identifier">type</span>     <span class="identifier">time_dimension</span><span class="special">;</span>
</pre>
<p>
    </p>
<p>
      This can also be easily accomplished using a convenience typedef provided by
      <span class="underline"><code class="computeroutput"><a class="link" href="../boost/units/base_dimension.html" title="Class template base_dimension">base_dimension</a></code></span>:
    </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">length_base_dimension</span><span class="special">::</span><span class="identifier">dimension_type</span>    <span class="identifier">length_dimension</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">mass_base_dimension</span><span class="special">::</span><span class="identifier">dimension_type</span>      <span class="identifier">mass_dimension</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">time_base_dimension</span><span class="special">::</span><span class="identifier">dimension_type</span>      <span class="identifier">time_dimension</span><span class="special">;</span>
</pre>
<p>
    </p>
<p>
      so that the above code is identical to the full typelist definition. Composite
      dimensions are similarly defined via a typelist:
    </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">make_dimension_list</span><span class="special">&lt;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span> <span class="identifier">dim</span><span class="special">&lt;</span> <span class="identifier">length_base_dimension</span><span class="special">,</span><span class="identifier">static_rational</span><span class="special">&lt;</span><span class="number">2</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="special">&gt;</span>
<span class="special">&gt;::</span><span class="identifier">type</span>   <span class="identifier">area_dimension</span><span class="special">;</span>

<span class="keyword">typedef</span> <span class="identifier">make_dimension_list</span><span class="special">&lt;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span> <span class="identifier">dim</span><span class="special">&lt;</span> <span class="identifier">mass_base_dimension</span><span class="special">,</span><span class="identifier">static_rational</span><span class="special">&lt;</span><span class="number">1</span><span class="special">&gt;</span> <span class="special">&gt;,</span>
                      <span class="identifier">dim</span><span class="special">&lt;</span> <span class="identifier">length_base_dimension</span><span class="special">,</span><span class="identifier">static_rational</span><span class="special">&lt;</span><span class="number">2</span><span class="special">&gt;</span> <span class="special">&gt;,</span>
                      <span class="identifier">dim</span><span class="special">&lt;</span> <span class="identifier">time_base_dimension</span><span class="special">,</span><span class="identifier">static_rational</span><span class="special">&lt;-</span><span class="number">2</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="special">&gt;</span>
<span class="special">&gt;::</span><span class="identifier">type</span>    <span class="identifier">energy_dimension</span><span class="special">;</span>
</pre>
<p>
    </p>
<p>
      A convenience class for composite dimensions with integer powers is also provided:
    </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">derived_dimension</span><span class="special">&lt;</span><span class="identifier">length_base_dimension</span><span class="special">,</span><span class="number">2</span><span class="special">&gt;::</span><span class="identifier">type</span>  <span class="identifier">area_dimension</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">derived_dimension</span><span class="special">&lt;</span><span class="identifier">mass_base_dimension</span><span class="special">,</span><span class="number">1</span><span class="special">,</span>
                          <span class="identifier">length_base_dimension</span><span class="special">,</span><span class="number">2</span><span class="special">,</span>
                          <span class="identifier">time_base_dimension</span><span class="special">,-</span><span class="number">2</span><span class="special">&gt;::</span><span class="identifier">type</span>   <span class="identifier">energy_dimension</span><span class="special">;</span>
</pre>
<p>
    </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2003-2008 Matthias Christian Schabel<br>Copyright © 2007-2010 Steven
      Watanabe<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="Quick_Start.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_units.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="Units.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
